package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.domain.TripType;
import com.ruoyi.system.service.ITripTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出行类型Controller
 *
 * @author trip
 * @date 2024-09-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/triptype")
public class TripTypeController extends BaseController {

    private final ITripTypeService tripTypeService;

/**
 * 查询出行类型列表
 */
@PreAuthorize("@ss.hasPermi('system:triptype:list')")
@GetMapping("/list")
    public TableDataInfo list(TripType tripType) {
        startPage();
        LambdaQueryWrapper<TripType> lqw = new LambdaQueryWrapper<TripType>();
                        if (StringUtils.isNotBlank(tripType.getName())){
                        lqw.like(TripType::getName ,tripType.getName());
                    }
                        if (StringUtils.isNotBlank(tripType.getStatus())){
                        lqw.eq(TripType::getStatus ,tripType.getStatus());
                    }
        List<TripType> list = tripTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TripType> util = new ExcelUtil<TripType>(TripType.class);
        util.importTemplateExcel(response, "出行类型数据");
    }

    /**
     * 导入出行类型数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TripType> util = new ExcelUtil<TripType>(TripType.class);
        List<TripType> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            tripTypeService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出出行类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:export')")
    @Log(title = "出行类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TripType tripType) {
        List<TripType> list = tripTypeService.selectTripTypeList(tripType);
        ExcelUtil<TripType> util = new ExcelUtil<TripType>(TripType.class);
        util.exportExcel(response, list, "出行类型数据");
    }

    /**
     * 获取出行类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:query')")
    @GetMapping(value = "/{tripTypeId}")
    public AjaxResult getInfo(@PathVariable("tripTypeId") Long tripTypeId) {
        return AjaxResult.success(tripTypeService.selectTripTypeByTripTypeId(tripTypeId));
    }

    /**
     * 新增出行类型
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:add')")
    @Log(title = "出行类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TripType tripType) {
        return toAjax(tripTypeService.insertTripType(tripType));
    }

    /**
     * 修改出行类型
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:edit')")
    @Log(title = "出行类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TripType tripType) {
        return toAjax(tripTypeService.updateTripType(tripType));
    }

    /**
     * 删除出行类型
     */
    @PreAuthorize("@ss.hasPermi('system:triptype:remove')")
    @Log(title = "出行类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tripTypeIds}")
    public AjaxResult remove(@PathVariable Long[] tripTypeIds) {
        return toAjax(tripTypeService.deleteTripTypeByTripTypeIds(tripTypeIds));
    }
}


